#include "signin.h"
#include "ui_signin.h"
#include "studentstore.h"
#include <QString>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <string.h>
#include <QMessageBox>
#include <QTextCodec>
#include "login.h"
#include "importstudent.h"
#include "show_stuinfo.h"
#include "debug.h"
#include "test.h"
#include "utils.h"

using namespace std;
using namespace student;
using namespace studentstore;
using namespace utils;
signin::signin(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::signin) {
    ui->setupUi(this);
//    logino = new login(this);
//    logino->hide();
    importStudentUI = new importStudent(this);
    importStudentUI->hide();
    show_stulistUI = new show_stulist(this);
    show_stulistUI->hide();
    deBugUI = new deBug(this);
    deBugUI->hide();
    testo = new test(this);
    testo->hide();
    ui->pushButton_2->hide();
    ui->pushButton_2->setEnabled(false);
    ui->pushButton_3->hide();
    ui->pushButton_3->setEnabled(false);
    ui->pushButton_4->hide();
    ui->pushButton_4->setEnabled(false);
    ui->pushButton_5->hide();
    ui->pushButton_5->setEnabled(false);
//    ui->deBugButton->hide();
//    ui->deBugButton->setEnabled(false);
    QFile textFile(majorDatPath);
    textFile.open(QIODevice::ReadOnly);
    QTextStream textStream(&textFile);
    textStream.setCodec(QTextCodec::codecForName("UTF-8"));
    while (true) {
        QString line = textStream.readLine();
        if (line.isNull())
            break;
        else
            stringList.append(line);
    }

    ui->comboBox->addItems(stringList);//把列表加载到comboBox
    // QString s = QString::number(stringList.size());//major个数

}

signin::~signin() {
    delete ui;
}

void signin::on_pushButton_clicked() {
    QString str = ui->lineEdit->text();
    string stu_id = string((const char *)str.toLocal8Bit());
    if(stu_id.size() == 0) {
        QMessageBox::warning(this, "warning", "请输入账号，为您的学号");
        return;
    }
    else if(strcmp(stu_id.data(),"888888")==0){
        QMessageBox::warning(this, "warning", "我预判了你的预判hhhhh不能注册这个！！！！！！");
        return;
    }
    else if(judge_user(stu_id) == -1) {
        QMessageBox::warning(this, "warning", "账号格式有误");
        return;
    }

    else
        ;
    QString str2 = ui->lineEdit_2->text();
    string name = string((const char *)str2.toLocal8Bit());
    if(name.size() == 0) {
        QMessageBox::warning(this, "warning", "请输入姓名");
        return;
    } else if(name.length() < 4) {
        QMessageBox::warning(this, "warning", "姓名长度需要大于4字节");
        return;
    } else if(name.length() > 36) {
        QMessageBox::warning(this, "warning", "姓名长度需要小于36字节");
        return;
    } else
        ;
    QString str3 = ui->lineEdit_3->text();
    string pwd = string((const char *)str3.toLocal8Bit());
    if(pwd.size() == 0) {
        QMessageBox::warning(this, "warning", "请输入密码");
        return;
    } else if(pwd.length() < 6) {
        QMessageBox::warning(this, "warning", "密码长度不得小于6字节");
        return;
    } else if(pwd.length() > 16) {
        QMessageBox::warning(this, "warning", "密码长度不得大于16字节");
        return;
    } else
        ;
    QString comb = ui->comboBox->currentText();
    string major = string((const char *)comb.toLocal8Bit());
    vector<Student*>stulist = search_multipleReturn("notFuzzySearch", "stu_id", stu_id, studentDatPath);
    if(stulist.size() != 0) {
        QMessageBox::warning(this, "warning", "该用户已注册");
        return;
    }
    //hash密码
    string salt;        //生成盐字符串
    salt = rand_str(5);
    string pwdHash = sha1(sha1(pwd) + salt) ;

    Student* stu = new Student ();
    stu = creatStudent(name, stu_id, pwdHash, salt, major);
    addStudent(stu);
    QMessageBox::information(this, "info", "提交成功");
    delete stu;
    this->hide();
}

void signin::on_pushButton_2_clicked() {
    importStudentUI->open();
}

void signin::on_pushButton_3_clicked() {
    show_stulistUI->open();
}
void signin::on_deBugButton_clicked() {
    deBugUI->open();
}

void signin::on_pushButton_4_clicked() {
    testo->open();
}

void signin::on_pushButton_5_clicked() {
//   admin->open();
}

